<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\coachbroker\controller;

class AdminBroker extends \app\AdminRest
{
    protected $model;
    protected $user_model;
    protected $cash_model;
    protected $wallet_model;
    protected $level_model;
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
        $this->model = new \app\coachbroker\model\CoachBroker();
        $this->user_model = new \app\massage\model\User();
        $this->cash_model = new \app\massage\model\Commission();
        $this->wallet_model = new \app\massage\model\Wallet();
        $this->level_model = new \app\coachbroker\model\BrokerLevel();
    }
    public function brokerList()
    {
        $input = $this->_param;
        $dis[] = ["a.uniacid", "=", $this->_uniacid];
        if(!empty($input["status"])) {
            $dis[] = ["a.status", "=", $input["status"]];
        } else {
            $dis[] = ["a.status", ">", -1];
        }
        if(!empty($input["start_time"]) && !empty($input["end_time"])) {
            $start_time = $input["start_time"];
            $end_time = $input["end_time"];
            $dis[] = ["a.create_time", "between", $start_time . "," . $end_time];
        }
        $where = [];
        if(!empty($input["name"])) {
            $where[] = ["a.user_name", "like", "%" . $input["name"] . "%"];
            $where[] = ["a.mobile", "like", "%" . $input["name"] . "%"];
        }
        $data = $this->model->adminDataList($dis, $input["limit"], $where);
        $level_model = new \app\coachbroker\model\BrokerLevel();
        if(!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                $v["balance"] = $level_model->getBrokerBalance($v["id"], $v["uniacid"]);
            }
        }
        $list = ["all", "ing", "pass", "4" => "nopass"];
        foreach ($list as $k => $value) {
            $dis_s = [];
            $dis_s[] = ["uniacid", "=", $this->_uniacid];
            if(!empty($k)) {
                $dis_s[] = ["status", "=", $k];
            } else {
                $dis_s[] = ["status", ">", -1];
            }
            $data[$value] = $this->model->where($dis_s)->count();
        }
        return $this->success($data);
    }
    public function brokerInfo()
    {
        $input = $this->_param;
        $dis = ["id" => $input["id"]];
        $info = $this->model->dataInfo($dis);
        $user_model = new \app\massage\model\User();
        $info["nickName"] = $user_model->where(["id" => $info["user_id"]])->value("nickName");
        return $this->success($info);
    }
    public function brokerAdd()
    {
        $input = $this->_input;
        $find = $this->model->where(["user_id" => $input["user_id"]])->where("status", ">", -1)->find();
        if(!empty($find) && in_array($find["status"], [1, 2, 3])) {
            $this->errorMsg("已经申请过经纪人了");
        }
        $insert = ["uniacid" => $this->_uniacid, "user_id" => $input["user_id"], "user_name" => $input["user_name"], "true_user_name" => !empty($input["true_user_name"]) ? $input["true_user_name"] : $input["user_name"], "mobile" => $input["mobile"], "text" => $input["text"], "sh_time" => time(), "status" => 2];
        if(!empty($find) && $find["status"] == 4) {
            $res = $this->model->dataUpdate(["id" => $find["id"]], $insert);
        } else {
            $res = $this->model->dataAdd($insert);
        }
        return $this->success($res);
    }
    public function brokerUpdate()
    {
        $input = $this->_input;
        $diss = ["id" => $input["id"]];
        $info = $this->model->dataInfo($diss);
        if(!empty($input["status"]) && in_array($input["status"], [2, 4, -1])) {
            if($info["status"] == 1) {
                $input["sh_time"] = time();
            }
            if($input["status"] != -1 || 0 < $info["cash"]) {
            }
        }
        if(isset($input["cash"])) {
            unset($input["cash"]);
        }
        $data = $this->model->dataUpdate($diss, $input);
        return $this->success($data);
    }
    public function brokerDataList()
    {
        $input = $this->_param;
        $dis[] = ["a.uniacid", "=", $this->_uniacid];
        $dis[] = ["b.status", "=", 2];
        $where = [];
        if(!empty($input["name"])) {
            $where[] = ["a.nickName", "like", "%" . $input["name"] . "%"];
            $where[] = ["b.user_name", "like", "%" . $input["name"] . "%"];
            $where[] = ["b.mobile", "like", "%" . $input["name"] . "%"];
        }
        if(!empty($input["start_time"]) && !empty($input["end_time"])) {
            $dis[] = ["b.sh_time", "between", $input["start_time"] . "," . $input["end_time"]];
        }
        $data = $this->model->userDataList($dis, $where, $input["limit"]);
        return $this->success($data);
    }
    public function noBrokerUserList()
    {
        $input = $this->_param;
        $user_model = new \app\massage\model\User();
        $user_id = $this->model->where(["uniacid" => $this->_uniacid])->where("status", "in", [1, 2, 3])->column("user_id");
        $dis[] = ["id", "not in", $user_id];
        $dis[] = ["status", "=", 1];
        $where = [];
        if(!empty($input["name"])) {
            $where[] = ["nickName", "like", "%" . $input["name"] . "%"];
            $where[] = ["phone", "like", "%" . $input["name"] . "%"];
        }
        $data = $user_model->dataList($dis, $input["limit"], $where, "id,nickName,avatarUrl,phone");
        return $this->success($data);
    }
    public function brokerCoachList()
    {
        $input = $this->_param;
        $dis[] = ["status", "=", 2];
        $dis[] = ["broker_id", "=", $input["id"]];
        if(!empty($input["coach_name"])) {
            $dis[] = ["coach_name", "like", "%" . $input["coach_name"] . "%"];
        }
        $coach_model = new \app\massage\model\Coach();
        $data = $coach_model->where($dis)->field("admin_id,id,coach_name,work_img,city_id,sh_time")->order("partner_time desc,id desc")->paginate($input["limit"])->toArray();
        if(!empty($data["data"])) {
            $config_model = new \app\massage\model\Config();
            $level_model = new \app\massage\model\CoachLevel();
            $city_model = new \app\massage\model\City();
            $admin_model = new \app\massage\model\Admin();
            $config = $config_model->dataInfo(["uniacid" => $this->_uniacid]);
            $level_cycle = $config["level_cycle"];
            foreach ($data["data"] as &$v) {
                $v["city"] = $city_model->where(["id" => $v["city_id"], "status" => 1])->value("city");
                $admin = $admin_model->dataInfo(["id" => $v["admin_id"], "status" => 1]);
                $v["admin_name"] = !empty($admin) ? $admin["agent_name"] : "平台";
                $v["order_price"] = $level_model->getMinPrice($v["id"], $level_cycle, 0, 1, $input["id"]);
            }
        }
        return $this->success($data);
    }
    public function configInfo()
    {
        $arr = ["broker_apply_port", "broker_cash_type", "broker_poster", "coach_agent_balance", "partner_coach_balance", "partner_admin_balance"];
        $data = getConfigSettingArr($this->_uniacid, $arr);
        return $this->success($data);
    }
    public function configUpdate()
    {
        $input = $this->_input;
        $config_model = new \app\massage\model\ConfigSetting();
        $arr = ["broker_apply_port", "broker_cash_type", "broker_poster", "coach_agent_balance", "partner_coach_balance", "partner_admin_balance"];
        foreach ($input as $key => $value) {
            if(!in_array($key, $arr)) {
                $this->errorMsg("请求不合法");
            }
        }
        $res = $config_model->dataUpdate($input, $this->_uniacid);
        return $this->success($res);
    }
    public function levelList()
    {
        $input = $this->_param;
        $this->level_model->initTop($this->_uniacid);
        $dis[] = ["uniacid", "=", $this->_uniacid];
        $dis[] = ["status", "=", 1];
        $dis = ["uniacid" => $this->_uniacid, "status" => 1];
        $data = $this->level_model->dataList($dis, $input["limit"]);
        if(!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                $v["lower"] = $this->level_model->where($dis)->where("inv_num", "<", $v["inv_num"])->max("inv_num");
            }
        }
        return $this->success($data);
    }
    public function levelAdd()
    {
        $input = $this->_input;
        $input["uniacid"] = $this->_uniacid;
        $dis = ["uniacid" => $this->_uniacid, "status" => 1];
        $arr = ["inv_num" => "邀请技师数量不能相同"];
        foreach ($arr as $k => $value) {
            $find = $this->level_model->where($dis)->where([$k => $input[$k]])->find();
            if(!empty($find)) {
                $this->errorMsg($value);
            }
        }
        $res = $this->level_model->dataAdd($input);
        return $this->success($res);
    }
    public function levelUpdate()
    {
        $input = $this->_input;
        $dis = ["id" => $input["id"]];
        if(isset($input["inv_num"])) {
            $diss = ["uniacid" => $this->_uniacid, "status" => 1];
            $arr = ["inv_num" => "邀请技师数量不能相同"];
            foreach ($arr as $k => $value) {
                $find = $this->level_model->where($diss)->where("id", "<>", $input["id"])->where([$k => $input[$k]])->find();
                if(!empty($find)) {
                    $this->errorMsg($value);
                }
            }
        }
        $res = $this->level_model->dataUpdate($dis, $input);
        return $this->success($res);
    }
    public function levelInfo()
    {
        $input = $this->_input;
        $dis = ["id" => $input["id"]];
        $res = $this->level_model->dataInfo($dis);
        return $this->success($res);
    }
    public function setBrokerBalance()
    {
        $input = $this->_input;
        $dis[] = ["id", "in", $input["id"]];
        $res = $this->model->dataUpdate($dis, ["balance" => $input["balance"]]);
        return $this->success($res);
    }
}

?>